<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻列表</title>
    <style>
        .news-box {
            width: 600px;
            margin: auto;
        }

        .news-item {
            display: flex;
            height: 120px;
            padding: 20px 0;
            cursor: pointer;
        }

        .news-item-left {
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            padding-right: 10px;
        }

        .news-item-left .title {
            font-size: 20px;
        }

        .news-item-left .info {
            color: #999999;
        }

        .news-item-left .info span {
            margin-right: 20px;
        }

        .news-item-right {
            width: 160px;
            height: 120px;
        }

        .news-item-right img {
            width: 100%;
            height: 100%;
        }

        .loading:before{
            position: absolute;
            top: 0;
            left: 0;
            content: '';
            width: 100%;
            height: 100%;
            background:#fff url("images/loading.gif") no-repeat center;

        }
    </style>
</head>

<!-- 引入vue插件 -->
 <script src="./js/vue@3.5.13.js"></script>
 <!-- 引入axios插件     用于网络请求-->
 <script src="./js/axios.js"></script>
<body>
    <div id="app">
        <div class="news-box" v-loading="">
            <div class="news-item" v-for="item in newList" :key="item.id">
                <!-- 左边 -->
                <div class="news-item-left">
                    <div class="title">{{ item.title }}</div>
                    <div class="info">
                        <span>{{item.source}}</span>
                        <span>{{ item.time }}</span>
                    </div>
                </div>
                <!-- 右边 -->
                <div class="news-item-right">
                    <img v-bind:src="item.img" alt="">     <!--v-bind为单向数据绑定，可以简写为‘ ：’-->
                </div>
            </div>
        </div>
    </div>

    <script>
        const { createApp,onMounted,ref } = Vue
        const app = createApp({
            setup(){
                //声明新闻列表
                let newList = ref([])
                //声明是否加载
                let isLoading = ref(true)      //一开始就要有加载
                //生命周期函数（专业术语：钩子函数）
                onMounted(async() => {           //async:表示同步进行
                    //请求接口（新闻列表）
                    let res = await axios.get("http://hmajax.itheima.net/api/news")
                    newList.value = res.data.data
                    isLoading.value = false     //请求成功后，
                })
                return { newList, isLoading }
            }
        })
        
        //自定义loading指令
        app.directive('loading',{
            //显示加载效果，el -->document.querySelector(".news-box")
            mounted(el,binding){        //页面加载后
                el.classList.add("loading")
            },
            //页面更新后
            updated(el,binding){
                el.classList.remove("loading")
            }
        })

        app.mount("#app")
    </script>
</body>

</html>